package com.system.service.impl;

import com.system.mapper.InvoiceMapper;
import com.system.mapper.ProductMapper;
import com.system.mapper.PurchaseMapper;
import com.system.mapper.SysUserMapper;
import com.system.pojo.Product;
import com.system.pojo.Purchase;
import com.system.pojo.SysUser;
import com.system.service.InvoiceService;
import com.system.service.PurchaseService;
import com.system.utils.UUIDUtil;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private InvoiceService invoiceService;

    @Autowired
    private InvoiceMapper invoiceMapper;

    @Override
    public ResultVo<Purchase> selectAll() {
        return null;
    }

    @Override
    public ResultVo<Purchase> selectAllDone() {
        ResultVo<Purchase> resultVo = new ResultVo<>();
        List<Purchase> purchases = purchaseMapper.selectAllDone();
        for (Purchase purchase : purchases) {
            Integer storeAdminId = purchase.getStoreAdminId();
            Integer productId = purchase.getProductId();
            SysUser sysUser = sysUserMapper.selectByPrimaryKey(storeAdminId);
            Product product = productMapper.selectByPrimaryKey(productId);
            purchase.setSysUser(sysUser);
            purchase.setProduct(product);
        }
        resultVo.setSelect(true);
        resultVo.setTList(purchases);
        resultVo.setMessage("查询成功");
        return resultVo;
    }

    @Override
    public ResultVo<Purchase> selectAllUndone() {
        ResultVo<Purchase> resultVo = new ResultVo<>();
        List<Purchase> purchases = purchaseMapper.selectAllUndone();
        System.out.println(purchases);
        for (Purchase purchase : purchases) {
            Integer productId = purchase.getProductId();
            Product product = productMapper.selectByPrimaryKey(productId);
            purchase.setProduct(product);
        }
        resultVo.setSelect(true);
        resultVo.setTList(purchases);
        resultVo.setMessage("查询成功");
        return resultVo;
    }

    @Override
    public ResultVo<Purchase> updateById(Integer id, Integer purchaseId) {
        ResultVo<Purchase> resultVo = new ResultVo<>();
        Purchase purchase = purchaseMapper.selectByPrimaryKey(purchaseId);
//        Product product = productMapper.selectByPrimaryKey(purchase.getProductId());
        Integer invoiceId = purchase.getInvoiceId();
        purchase.setStoreAdminId(id);
        purchase.setPurchaseTime(new Date());
        purchase.setStatus(true);
        int i = purchaseMapper.updateByPrimaryKeySelective(purchase);
        if (i == 1){
            if (invoiceId != null){
                invoiceService.update(id, invoiceId);
            }else {
            	Integer productId = purchase.getProductId();
            	Product product = productMapper.selectByPrimaryKey(productId);
            	product.addRepertory(purchase.getCount());
            	productMapper.updateByPrimaryKeySelective(product);
            }
			resultVo.setUpdate(true);
			resultVo.setMessage("进货成功");
		}else{
			resultVo.setMessage("进货失败");
		}
        return resultVo;
    }

    @Override
    public ResultVo<Purchase> insert(Product product) {
        ResultVo<Purchase> resultVo = new ResultVo<Purchase>();
        Integer purchaseNum = product.getPurchaseNum();
        Purchase p = new Purchase();
        p.setProductId(product.getId());
        p.setCount(product.getPurchaseNum());
        p.setPurchasePrice(product.getPrice());
        p.setPurchaseNum(UUIDUtil.uniqueString(20));
        purchaseMapper.insertSelective(p);
        resultVo.setInsert(true);
        resultVo.setMessage("生成进货单成功");
        return resultVo;
    }
}
